from entity.Student import Student
from entity.User import User

user = User
from dbUtils import Db_Connection as db
import Index
import random
import PySimpleGUI as sg

student = Student


# 查询操作
def selectAllStudent():
    sql = "SELECT * FROM students;"
    data = db.getData(sql)
    tit = ['序号', '学号', '姓名', '性别', '年龄', '家庭住址']
    return [data, tit]


# 添加学生
def insertStudent(window_ap):
    while True:
        event, values = window_ap.read()  # 窗口的读取，有两个返回值(1.事件  2.值)
        student.name = values['-student_name-']
        student.sex = values['-student_sex-']
        student.age = values['-student_age-']
        student.address = values['-student_address-']
        student.studentNum = str(random.randint(1000, 9999))
        if event == None:  # 窗口关闭事件
            break
        if event == '-cancel-':
            window_ap.close()
            Index.main()
            break
        if event == '-return-':
            window_ap.close()
            Index.main()
            break
        if event == '-ok-':
            sql = (
                    "INSERT INTO students(num,name,sex,age,address) VALUES(" + student.studentNum + ",'" + student.name + "','" + student.sex + "'," + student.age + ",'" + student.address + "');")
            db.writeData(sql)
            sg.popup('学生添加成功！')
            window_ap.close()
            Index.main()
            break


# 删除学生
def deleteStudent(window_dp):
    while True:
        event, values = window_dp.read()  # 窗口的读取，有两个返回值(1.事件  2.值)
        student.studentNum = values['-student_num_']
        if event == None:  # 窗口关闭事件
            break
        if event == '-cancel-':
            continue
        if event == '-return-':
            window_dp.close()
            Index.main()
            break
        if event == '-ok-':
            sql = "DELETE FROM students WHERE num=" + student.studentNum + ";"
            r = db.writeData(sql)
            if r == 0:
                sg.popup('删除失败')
            else:
                sg.popup('学生', student.studentNum, '删除成功！')
                window_dp.close()
                Index.main()
                break


# 修改学生
def updateStudent(window_sd):
    while True:
        event, values = window_sd.read()  # 窗口的读取，有两个返回值(1.事件  2.值)
        if event == None:  # 窗口关闭事件
            break
        if event == '-cancel-':
            window_sd.close()
            Index.main()
            break
        if event == '-return-':
            window_sd.close()
            Index.main()
            break
        if event == '-ok-':
            student.studentNum = values['-student_num-']
            student.name = values['-student_name-']
            student.sex = values['-student_sex-']
            student.age = values['-student_age-']
            student.address = values['-student_address-']
            sql = "UPDATE students SET name=" + student.name + ','"sex=" + student.sex + ','"age=" + student.age + ','"address=" + student.address + " WHERE num=" + student.studentNum + ";"
            r = db.writeData(sql)
            if r == 0:
                sg.popup('修改失败！')
            else:
                sg.popup('学生', student.studentNum, '修改学生成功！')
                window_sd.close()
                Index.main()
                break
